# encoding: utf-8

class Bug < ActiveRecord::Base
  belongs_to  :project,
              :touch =>  true

  belongs_to  :topic,
              :touch => true

  belongs_to  :author,
              :class_name => "User",
              :foreign_key => "author_id"

  belongs_to  :processor,
              :class_name => "User",
              :foreign_key => "processor_id"
  
  has_many    :dataobjects

  #validations
  validates :project_id,
            :presence     =>  true,
            :numericality =>  {  :only_integer => true,
                                 :greater_than => 0  }

  validates :topic_id,
            :numericality =>  { :only_integer =>  true,
                                :greater_than =>  0,
                                :allow_nil    =>  true  }
              
  validates :author_id,
            :presence     =>  true,
            :numericality =>  { :only_integer => true,
                                :greater_than => 0  }

  validates :processor_id,
            :presence     =>  true,
            :numericality =>  { :only_integer => true,
                                :greater_than => 0  }

  validates :title, :presence => true
  validates :title, :length   => { :within => 5..255 }, :unless => "self.title.empty?"

  validates :description, :presence => true
  validates :description, :length   => { :within => 5..500 }, :unless => "self.description.empty?"


  validates :status,
            :presence  => true,
            :inclusion => { :in => ['open', 'closed', 'reopened', 'invalid', 'in_progress', 'retest_needed'] }

  validates :priority,
            :presence   =>  true,
            :inclusion  =>  {  :in => ['low', 'medium', 'high', 'critical'] }
end


# == Schema Information
#
# Table name: bugs
#
#  id           :integer         not null, primary key
#  title        :string(255)     default(""), not null
#  project_id   :integer
#  topic_id     :integer
#  author_id    :integer
#  processor_id :integer
#  description  :text
#  status       :string(255)     default("open")
#  priority     :string(255)     default("low")
#  created_at   :datetime
#  updated_at   :datetime
#

